package com.xique.door.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xique.common.core.constant.OssFilePrefixConstants;
import com.xique.common.core.domain.R;
import com.xique.common.core.enums.StatusCode;
import com.xique.common.core.utils.AliossUtil;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.core.web.domain.PageResult;
import com.xique.door.bean.domain.OssfileUrl;
import com.xique.door.bean.domain.SysUser;
import com.xique.door.mapper.OssfileUrlMapper;
import com.xique.door.service.IOssfileUrlService;
import com.xique.door.service.ISysUserService;
import com.xique.door.util.DoorAliossUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Log4j2
public class OssfileUrlServiceImpl extends ServiceImpl<OssfileUrlMapper, OssfileUrl>
    implements IOssfileUrlService {
  @Autowired private DoorAliossUtil aliossUtil;

  @Autowired private ISysUserService sysUserService;

  @Override
  @Async
  public void uploadMineVideo(MultipartFile file, OssfileUrl oss) throws Exception {
    Map<String, Object> resultMap = null;
    String prefix = null;
    if (oss.getFileType() == 1) {
      log.info("图片上传开始");
      prefix = OssFilePrefixConstants.OPERATE_IMGAGE;
      resultMap = aliossUtil.uploadFile(file, prefix);
    } else if (oss.getFileType() == 2) {
      log.info("视频上传开始");
      prefix = OssFilePrefixConstants.OPERATE_VIDEO;
      resultMap = aliossUtil.uploadFileTranscoding(file, prefix);
    } else if (oss.getFileType() == 3) {
      log.info("安装包上传开始");
      prefix = OssFilePrefixConstants.INSTALLATION_PACKAGE;
      resultMap = aliossUtil.uploadFile(file, prefix);
    }
    if (resultMap.get("code").equals(StatusCode.ERROR.getCode())) {
      log.info(" 文件上传失败");
    }
    AjaxResult result = AjaxResult.success("success", "文件上传成功");
    result.put("url", resultMap.get("url"));
    result.put("objectName", resultMap.get("objectName"));
    log.info("上传结束" + resultMap.get("objectName"));

    oss.setName(resultMap.get("objectName").toString());
    oss.setUrl(resultMap.get("url").toString());

    addOssfileUr(oss);
  }

  @Override
  public PageResult getOssfileUrlList(
      Integer fileType,
      String title,
      Integer pageNum,
      Integer pageSize,
      String startTime,
      String endTime,
      String type,
      String model,String userName) {
    QueryWrapper<OssfileUrl> wrapper = new QueryWrapper<>();
    if (title != null && !"".equals(title)) {
      wrapper.like("title", title);
    }
    if (fileType != null) {
      wrapper.eq("file_type", fileType);
      if (fileType != 3) {
        wrapper.isNull("type");
      } else if (fileType == 3) {
        if (type != null&&!"".equals(type)) {
          wrapper.eq("type", type);
        }
        if (model != null && !"".equals(model)) {
          wrapper.eq("model", model);
        }
      }
    }
    if (startTime != null && !"".equals(startTime) && endTime != null && !"".equals(endTime)) {
      wrapper.between("update_time", startTime, endTime);
    }
    if (startTime != null && !"".equals(startTime)) {
      wrapper.ge("update_time", startTime);
    }

    if (userName != null && !"".equals(userName)) {
      SysUser sysUser = sysUserService.getSysUser(null, userName);
      if (sysUser != null) {
        wrapper.eq("update_by", sysUser.getUserId());
      } else {
        wrapper.isNull("update_by");
      }
    }

    wrapper.eq("del_flag", 0);
    wrapper.orderByDesc("update_time");
    Page<OssfileUrl> page = new Page<>();
    page.setCurrent(pageNum);
    page.setSize(pageSize);

    IPage<OssfileUrl> list = baseMapper.selectPage(page, wrapper);

    long totalPages = list.getPages();
    long totalSize = list.getTotal();

    List<OssfileUrl> lists = list.getRecords();
    if (lists.size() > 0) {
      for (OssfileUrl ossfileUrl : lists) {
        SysUser sysUser = sysUserService.getSysUser(ossfileUrl.getUpdateBy(), null);
        if (sysUser != null) {
          ossfileUrl.setUpdateUserName(sysUser.getUserName());
        }
      }
    }

    return PageResult.builder()
        .pageNum(pageNum)
        .pageSize(pageSize)
        .totalPages(totalPages)
        .totalSize(totalSize)
        .list(lists)
        .build();
  }

  @Override
  public R addOssfileUr(OssfileUrl ossfileUrl) {
    ossfileUrl.setDelFlag(0);
    ossfileUrl.setCreateTime(new Date());
    int rep = baseMapper.insert(ossfileUrl);
    if (rep > 0) {
      return R.ok(null, "操作成功");
    }
    return R.fail("操作失败");
  }

  @Override
  public R ModifyOssfileUr(OssfileUrl ossfileUrl) {

    ossfileUrl.setUpdateTime(new Date());
    ossfileUrl.setUpdateBy(SecurityUtils.getUserId());
    int rep = baseMapper.updateById(ossfileUrl);
    if (rep > 0) {
      return R.ok(null, "操作成功");
    }
    return R.fail("操作失败");
  }

  @Override
  public R getOssfileUrById(OssfileUrl ossfileUrl) {

    OssfileUrl ossfileUrl1 = baseMapper.selectById(ossfileUrl.getId());
    if (ossfileUrl1 != null) {
      return R.ok(ossfileUrl1, "操作成功");
    }
    return R.fail("操作失败");
  }
}
